﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports ADOConnection
Imports HTLibrary

Public Class CLog

#Region "Attributes"
    Private _ID_Log As Integer
    Private _ID_Action As Integer
    Private _LogContent As String
    Private _LogDate As System.DateTime
    Private _ID_User As System.Int16
    Private _Ip As String
    Private _HostName As String
#End Region

#Region "Properties"

    Public Property ID_LOG() As Integer
        Get
            Return _ID_Log
        End Get
        Set(ByVal value As Integer)
            _ID_Log = value
        End Set
    End Property

    Public Property ID_ACTION() As Integer
        Get
            Return _ID_Action
        End Get
        Set(ByVal value As Integer)
            _ID_Action = value
        End Set
    End Property

    Public Property LOGCONTENT() As String
        Get
            Return _LogContent
        End Get
        Set(ByVal value As String)
            _LogContent = value
        End Set
    End Property

    Public Property LOGDATE() As System.DateTime
        Get
            Return _LogDate
        End Get
        Set(ByVal value As System.DateTime)
            _LogDate = value
        End Set
    End Property

    Public Property ID_USER() As System.Int16
        Get
            Return _ID_User
        End Get
        Set(ByVal value As System.Int16)
            _ID_User = value
        End Set
    End Property

    Public Property IP() As String
        Get
            Return _Ip
        End Get
        Set(ByVal value As String)
            _Ip = value
        End Set
    End Property

    Public Property HOSTNAME() As String
        Get
            Return _HostName
        End Get
        Set(ByVal value As String)
            _HostName = value
        End Set
    End Property

    Public Function Insert() As Long
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim objStringBuilder As New StringBuilder(250)
        With objStringBuilder
            .Append("INSERT INTO tblLog(" & _
                    "ID_Action, LogContent, LogDate, ID_User, Ip, HostName) VALUES(")
            .Append(objDBUtility.GetParamPlaceHolder("ID_Action", True))
            .Append(objDBUtility.GetParamPlaceHolder("LogContent", True))
            .Append(objDBUtility.GetParamPlaceHolder("LogDate", True))
            .Append(objDBUtility.GetParamPlaceHolder("ID_User", True))
            .Append(objDBUtility.GetParamPlaceHolder("Ip", True))
            .Append(objDBUtility.GetParamPlaceHolder("HostName", False))
            .Append(")")
        End With

        Dim sInsert As String = objStringBuilder.ToString

        Dim objCommand As IDbCommand = objConnection.CreateCommand(sInsert)
        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("ID_Action", DbType.Decimal, _ID_Action, 0))
                .Parameters.Add(objDBUtility.CreateParameter("LogContent", DbType.String, _LogContent, _LogContent.Length))
                .Parameters.Add(objDBUtility.CreateParameter("LogDate", DbType.Date, GetDateParam(_LogDate), 0))
                .Parameters.Add(objDBUtility.CreateParameter("ID_User", DbType.Decimal, _ID_User, 0))
                .Parameters.Add(objDBUtility.CreateParameter("Ip", DbType.String, _Ip, _Ip.Length))
                .Parameters.Add(objDBUtility.CreateParameter("Hostname", DbType.String, _HostName, _HostName.Length))
            End With
            _ID_Log = objDBUtility.InsertAndReturnSerialKey(objCommand, "ID_Log", "tblLog")
            objTransaction.Commit()
        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try
        Return _ID_Log
    End Function
#End Region

End Class

Public Class CLogs
    'Public Shared Function GetLogList(ByVal id_action As Integer, ByVal userName As String, ByVal logContent As String, ByVal fromDate As Date, ByVal toDate As Date) As ArrayList

    'End Function

    'Public Shared Function GetLogList(ByVal strDK As String) As ArrayList
    '    Dim aryItems As New ArrayList
    '    Dim strSQL As String
    '    Dim strWHERE As String = ""

    '    If strDK.Length > 0 Then
    '        strWHERE = " WHERE " & strDK
    '    End If

    '    strSQL = "SELECT id_log, id_action, logcontent, logdate, id_user, ip, hostname FROM tblLog"
    '    strSQL &= strWHERE
    '    strSQL &= " ORDER BY logdate desc"
    '    Dim objReader As SqlDataReader = Nothing
    '    Try
    '        Dim objItem As CLog
    '        objReader = BaseDB.ExecSql_DataReader(strSQL)
    '        While objReader.Read
    '            'objItem = GetPhieuNX(objReader)
    '            objItem = New CLog
    '            With objItem
    '                .ID_LOG = objReader.GetInt32(0)
    '                .ID_ACTION = objReader.GetInt32(1)
    '                .LOGCONTENT = objReader.GetString(2)
    '                .LOGDATE = objReader.GetValue(3)
    '                .ID_USER = objReader.GetInt16(4)
    '                .IP = objReader.GetValue(5)
    '                .HOSTNAME = objReader.GetValue(6)
    '            End With
    '            aryItems.Add(objItem)
    '        End While
    '    Catch ex As System.Exception
    '        Throw ex
    '    Finally
    '        If Not IsNothing(objReader) Then
    '            objReader.Close()
    '        End If
    '    End Try
    '    Return aryItems
    'End Function

    Public Shared Function GetLogList(ByVal strDK As String) As DataSet
        Dim ds As DataSet
        Dim strSQL As String
        Dim strWHERE As String = ""
        Try
            If strDK.Length > 0 Then
                strWHERE = " WHERE " & strDK
            End If

            strSQL = "SELECT * FROM viewHT_Log"
            strSQL &= strWHERE
            strSQL &= " ORDER BY logdate desc"

            ds = BaseDB.ExecSql_DataSet(strSQL)
        Catch ex As System.Exception
            Throw ex
        End Try
        Return ds
    End Function

    Public Shared Function InitLog() As CLog
        Dim objLog As New CLog
        objLog.ID_USER = g_User.User_ID
        objLog.LOGDATE = Now
       
        Dim objIPInfo As IPInfo = HTLibrary.CollectionHelper.GetIP()
        objLog.IP = objIPInfo.Ip
        objLog.HOSTNAME = objIPInfo.HostName
        Return objLog
    End Function

    ''' <summary>
    ''' Insert log
    ''' </summary>
    ''' <param name="idAction"></param>
    ''' <param name="logContent"></param>
    ''' <remarks></remarks>
    Public Shared Sub Insert(ByVal idAction As enumAction, Optional ByVal logContent As String = "")
        Dim objLog As CLog = InitLog()
        objLog.ID_ACTION = idAction
        objLog.LOGCONTENT = logContent
        objLog.Insert()
    End Sub

    ''' <summary>
    ''' Insert log cho phiếu NX
    ''' </summary>
    ''' <param name="idAction"></param>
    ''' <param name="objPhieu"></param>
    ''' <remarks></remarks>
    Public Shared Sub WriteLog_PhieuNX(ByVal idAction As enumAction, ByVal objPhieu As CPhieuNX)
        Dim objLog As CLog = InitLog()
        objLog.ID_ACTION = idAction

        If objPhieu.NX = LocNhapXuat(enumDOITUONG.TINHCHATNHAP) Then
            objLog.LOGCONTENT = "Phiếu nhập " + objPhieu.SOPHIEU
        Else
            objLog.LOGCONTENT = "Phiếu xuất " + objPhieu.SOPHIEU
        End If

        Select Case idAction
            Case enumAction.ThemMoi
                objLog.LOGCONTENT += " - Thêm mới"
            Case enumAction.Sua
                objLog.LOGCONTENT += " - Sửa"
            Case enumAction.Xoa
                objLog.LOGCONTENT += " - Xóa"
            Case enumAction.XacNhan
                objLog.LOGCONTENT += " - " + CBCTHs.GetBCTH(objPhieu.BCTH).DESC
            Case enumAction.HuyXacNhan
                objLog.LOGCONTENT += " - " + CBCTHs.GetBCTH_HuyXacNhan(objPhieu.BCTH).DESC
        End Select

        objLog.Insert()
    End Sub

    Public Shared Sub WriteLog_LoHang(ByVal idAction As enumAction, ByVal obj_MatHang As CMatHang)
        Dim objLog As CLog = InitLog()
        objLog.ID_ACTION = idAction

        objLog.LOGCONTENT = "Mặt hàng: " + obj_MatHang.Ten + " - " + obj_MatHang.Ma + "." + obj_MatHang.Code + " - " + obj_MatHang.Lo

        objLog.Insert()
    End Sub
End Class